package com.hyj.shop.project.login.service.impl;

import com.hyj.shop.project.user.domain.UserInfo;
import com.hyj.shop.project.user.mapper.UserInfoMapper;
import com.hyj.shop.project.utils.domain.AjaxResult;
import com.hyj.shop.project.login.domain.Login;
import com.hyj.shop.project.utils.domain.Msg;
import com.hyj.shop.project.user.domain.User;
import com.hyj.shop.project.login.mapper.LoginMapper;
import com.hyj.shop.project.login.service.LoginService;
import com.hyj.shop.utils.DateUtils;
import com.hyj.shop.utils.constant.Constants;
import com.hyj.shop.utils.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private RedisCache redisCache;
    @Resource
    RedisTemplate<String,Object> redisTemplate;
    @Override
    public AjaxResult login(Login login) {

        User data=loginMapper.login(login);
        Msg msg=new Msg();
        System.out.println("登录查到的数据");
        System.out.println(data);
        if(data!=null){
            String verifyKey = Constants.CAPTCHA_CODE_KEY + login.getUuid();
        String captcha = redisCache.getCacheObject(verifyKey);
            System.out.println(captcha);
            if(login.getVerificationCode().equalsIgnoreCase(captcha)){
                System.out.println("登录成功");
                String token= UUID.randomUUID()+"";
                Date date =new Date();
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String createTime=  df.format(date);
                System.out.println(date);
                redisTemplate.opsForValue().set(token,data, 3, TimeUnit.DAYS);
                Long seconds = redisTemplate.opsForValue().getOperations().getExpire(token);//此方法返回单位为秒过期时长
                System.out.println(seconds);
                 UserInfo userInfo= userInfoMapper.queryUserInfo(data.getUserId());
                AjaxResult ajax = AjaxResult.success();
                ajax.put("code", 200);
                ajax.put("token",token );
                ajax.put("createTime",createTime);
                ajax.put("userInfo",userInfo);
                ajax.put("timeOut",3);
                ajax.put("status","success");
                return ajax;
            }else {
                AjaxResult ajax =    AjaxResult.error("验证码错误");
                ajax.put("status","error");
             return    ajax;
            }
        }else {
            AjaxResult ajax =    AjaxResult.error("密码错误");
            ajax.put("status","error");
            return   ajax;
        }
    }

    @Override
    public AjaxResult register(User user) {
        try{
            Date date = DateUtils.getNowDate();
            user.setCreateTime(date);
            User userId = loginMapper.queryUser(user.getUserName()) ;
            if(userId==null){
                loginMapper.register(user);
                AjaxResult ajaxResult = AjaxResult.success();
                return ajaxResult;
            }else {
                AjaxResult ajaxResult = AjaxResult.error("该用户已存在！");
                return ajaxResult;
            }


        }catch (Exception e){
            System.out.println(e);
            AjaxResult ajaxResult = AjaxResult.error();
            return ajaxResult;

        }
    }
}
